package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.constant.Constant;
import com.demo.entity.VtePreventive;
import com.demo.entity.VteTabooScoreItem;
import com.demo.mapper.VtePreventiveMapper;
import com.demo.mapper.VteTabooScoreItemMapper;
import com.demo.response.Result;
import com.demo.service.IVteTabooScoreItemService;
import com.demo.util.TimeUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * VTE预防禁忌评论项 服务实现类
 *
 * @author zm
 * @since 2020-11-25
 */
@Service
public class VteTabooScoreItemServiceImpl extends ServiceImpl<VteTabooScoreItemMapper, VteTabooScoreItem> implements IVteTabooScoreItemService {
    @Resource
    VteTabooScoreItemMapper tabooScoreItemMapper;
    @Resource
    VtePreventiveMapper vtePreventiveMapper;

    /**
     * 新增vte禁忌项目
     *
     * @param tabooScoreItem 实体对象
     * @return
     */
    @Override
    public Result addVteTabooScoreItem(VteTabooScoreItem tabooScoreItem) {
        boolean flag = this.save(tabooScoreItem);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 更新vte禁忌项目
     *
     * @param tabooScoreItem 实体对象
     * @return
     */
    @Override
    public Result updateVteTabooScoreItem(VteTabooScoreItem tabooScoreItem) {
        if (tabooScoreItem.getVteItemId() == null || tabooScoreItem.getUpdatedUserId() == null) {
            return Result.error(Constant.KEY_ID);
        }
        boolean flag = this.updateById(tabooScoreItem);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * 删除vte禁忌项目
     *
     * @param vteItemId     vte禁忌项目ID
     * @param deletedUserId 删除人ID
     * @return
     */
    @Override
    public Result deleteVteTabooScoreItem(Integer vteItemId, Integer deletedUserId) {
        VteTabooScoreItem item = new VteTabooScoreItem();
        item.setVteItemId(vteItemId);
        item.setDeletedUserId(deletedUserId);
        item.setDeletedTime(new Date());
        boolean flag = this.updateById(item);
        if (flag == false) {
            return Result.error(Constant.DB_ERROR);
        }
        return Result.success();
    }

    /**
     * vte禁忌项目列表
     *
     * @param pageNumber  页码
     * @param pageSize    每页数量
     * @param vteItemName vte禁忌项目名称
     * @return
     */
    @Override
    public Result vteTabooScoreItemList(Integer pageNumber, Integer pageSize, String vteItemName, Integer vtePreventiveId, Boolean allItem) {
        IPage<VteTabooScoreItem> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<VteTabooScoreItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(TimeUtil.isString(vteItemName), "vte_item_name", vteItemName);
        queryWrapper.orderByAsc("vte_item_sort");
        if (allItem) {
            VtePreventive vtePreventive = vtePreventiveMapper.selectById(vtePreventiveId);
            // 当前评估记录医生尚未签字则只查询未删除的
            if (vtePreventive != null && vtePreventive.getSignTime() == null) {
                queryWrapper.isNull("deleted_user_id");
            } else if (vtePreventive != null && vtePreventive.getSignTime() != null) {
                // 当前评估记录护士已签字则签字时间前就已经被删除的项就过滤掉
                queryWrapper.and(wrapper -> wrapper.gt("deleted_time", vtePreventive.getSignTime()).or().isNull("deleted_time"));
            }
        } else {
            queryWrapper.isNull("deleted_user_id");
        }
        page = tabooScoreItemMapper.selectPage(page, queryWrapper);
        PageInfo<VteTabooScoreItem> pageInfo = new PageInfo<>(page.getRecords());
        pageInfo.setPages((int) page.getPages());
        pageInfo.setTotal(page.getTotal());
        if (pageNumber.equals(page.getPages())) {
            pageInfo.setIsLastPage(false);
        } else {
            pageInfo.setIsLastPage(true);
        }
        return Result.success(pageInfo);
    }
}
